package com.ywd.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywd.platform.dto.VehicleDto;
import com.ywd.platform.entity.VehicleEntity;
import com.ywd.platform.exception.RPage;
import com.ywd.platform.mapper.VehicleMapper;
import com.ywd.platform.page.VehiclePageDto;
import com.ywd.platform.vo.DeviceVo;
import com.ywd.platform.vo.VehicleVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * YWD
 * 2025/8/17
 */
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, VehicleEntity> {

    public RPage<List<VehicleVo>> getVehiclePage(VehiclePageDto dto) {
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<VehicleVo> list = this.baseMapper.getVehiclePage(dto);
        return RPage.ok(list, dto.getPage(), dto.getLimit(), page.getTotal());
    }

    public VehicleVo getVehicleDetails(Integer id) {
        return BeanUtil.copyProperties(this.getById(id), VehicleVo.class);
    }

    public Boolean saveVehicle(VehicleDto dto) {
        return save(BeanUtil.copyProperties(dto, VehicleEntity.class));
    }

    public Boolean updateVehicle(VehicleDto dto) {
        return updateById(BeanUtil.copyProperties(dto, VehicleEntity.class));
    }

    public Boolean deleteVehicle(Integer id) {
        return removeById(id);
    }

    public VehicleVo getVehicleByVehicleNo(String vehicleNo) {
        LambdaQueryWrapper<VehicleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VehicleEntity::getLicensePlate, vehicleNo);
        return BeanUtil.copyProperties(getOne(queryWrapper), VehicleVo.class);
    }

}
